#include <iostream>
using namespace std;
// 利用另一个对象的生命周期来管理资源
class Singleton
{
    friend class AutoRelease;

public:
    static Singleton *getInstance()
    {
        if (_pInstance == nullptr)
        {
            _pInstance = new Singleton();
        }
        return _pInstance;
    }

private:
    static Singleton *_pInstance;
};
Singleton *Singleton::_pInstance = nullptr;
class AutoRelease
{
public:
    AutoRelease(Singleton *s)
        : _s(s)
    {
    }
    ~AutoRelease(){
        cout<<"~AutoRelease" << endl;
        if(_s){
            delete _s;
            _s = nullptr;
        }
    }

private:
    Singleton *_s;
};
void test()
{
    AutoRelease ar(Singleton::getInstance());
}
int main()
{
    test();
    return 0;
}
